import express from 'express'
import { deepClone, s2t, t2s } from '../../../util/common.js';
import { randSql, getResult, listSql, likeSql } from '../../../util/operateSql.js';
import author_router from './author.js'
var router = express.Router();

const TABLE1 = 'resource.ci'
const TABLE2 = 'resource.poet_song'
const TABLE3 = 'resource.poet_tang'
const TABLE4 = 'resource.poet_tang300'

// 宋词
router.get('/ci',(req,res,next) => {
  let sql = ''
  if (req.query.title) {
    sql = likeSql(TABLE1,{title: req.query.title, pageSize: req.query.number})
  } else {
    sql = randSql(TABLE1,req.query.number || 1,'value')
  }
  getResult(sql).then((result) => {
    res.json(req.app.locals.success(result))
    
  }).catch((err) => {
    res.json(req.app.locals.error(err))
  });

})

// 宋诗
router.get('/poem_song',(req,res,next) => {
  let sql = ''
  if (req.query.title) {
    sql = likeSql(TABLE2,{title: s2t(req.query.title), pageSize: req.query.number})
  } else {
    sql = randSql(TABLE2,req.query.number || 1,'id')
  }
  getResult(sql).then((result) => {
    const handleResult = result.map(e => {
      const el = deepClone(e)
      delete el.paragraphs
      
      return {
        paragraphs: e.paragraphs.replace(/[\"\[\]\\\,]/g,'').replace(/。/g,'\n'),
        ...el
      }
    })
    res.json(req.app.locals.success(handleResult))
    
  }).catch((err) => {
    
    res.json(req.app.locals.error(err))
  });

})

//唐诗
router.get('/poem_tang',(req,res,next) => {
  let sql = ''
  if (req.query.title) {
    sql = likeSql(TABLE3,{title: s2t(req.query.title), pageSize: req.query.pageSize})
  } else {
    sql = randSql(TABLE3,req.query.pageSize || 1,'id')
  }
  getResult(sql).then((result) => {
    const handleResult = result.map(e => {
      const el = deepClone(e)
      delete el.paragraphs      
      return {
        paragraphs: e.paragraphs.replace(/[\"\[\]\\\,]/g,'').replace(/。/g,'\n'),
        ...el
      }
    })
    res.json(req.app.locals.success(handleResult))
    
  }).catch((err) => {
    res.json(req.app.locals.error(err))
  });

})


// 模糊获取标题
router.get('/poem_title',async (req,res,next) => {
  let sql1 = '',sql2='',sql3='',sql4=''

  try {
      const {name,title} = req.query
      sql1 = likeSql(TABLE1,{author: name? (name) : '',title: title?(title):'',pageSize:req.query.pageSize})
      sql2 = likeSql(TABLE2,{author: name? s2t(name) : '',title: title? s2t(title):'',pageSize:req.query.pageSize})
      sql3 = likeSql(TABLE3,{author: name? s2t(name) : '',title: title? s2t(title):'',pageSize:req.query.pageSize})
      sql4 = likeSql(TABLE4,{author: name? s2t(name) : '',title: title? s2t(title):'',pageSize:req.query.pageSize})

      Promise.all([getResult(sql1),getResult(sql2),getResult(sql3),getResult(sql4)]).then((result) => {
        const reVal = result.flat(1).map(e => {
          return {
            id:e.id,
            author: t2s(e.author || '未知'),
            title: t2s(e.title),
            paragraphs: t2s(e?.paragraphs || e?.content)?.replace(/[\"\[\]\\\,]/g,'').replace(/。/g,'\n')
          }
        })
        res.json(req.app.locals.success(reVal))
      })

  } catch (error) {
    
    res.send(req.app.locals.error(error))
  }


})


router.use('/author',author_router)

export default router